package com.fucius.study.leetcode;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

public class UnionFind {
    int[] a;

    public UnionFind(){
        a = new int[10];
        for (int i = 0; i < 10; i++) {
            a[i] = i;
        }
    }

    public UnionFind(int length){
        a = new int[length];
        for (int i = 0; i < length; i++) {
            a[i] = i;
        }
    }
    public int find(int i){
        if(a[i]!=i){
            i = find(a[i]);
        }
        return i;
    }
    public void union(int i,int j){
        int fi = find(i);
        int fj = find(j);
        if(fi>fj){
            a[fj]=fi;
        }
        if(fi<fj){
            a[fi]=fj;
        }

    }


    public int orchestraLayout(int num, int x, int y) {
        //从1圈开始记
        //1计算当前圈
        int k = Math.min(Math.min(x,y),Math.min(num-x-1,num-y-1));
        //2计算外圈总数count k=0  外圈等于0 count = 0;
//        int count = (int)(1L*((num*num - (num-2*k)*(num-2*k))%9L));
        int count = (num%9)*(num%9) - ((num-2*k)%9)*((num-2*k)%9) + 81;

        //在当前圈位置边长 num-2*k-1
        //计算x,y在当前圈 哪条边上
        if(x==k){
            //初始位置(k,k)
            count += 0*(num-2*k-1) + (y-k+1);
        }else if(y == num-k-1){
            count += 1*((num-2*k-1)%9) + ((x-k+1)%9);
        }else if(x == num-k-1){
            count += 3*((num-2*k-1)%9) - ((y-k-1)%9) + 9;
        }else if(y==k){
            count += 4*((num-2*k-1)%9) - ((x-k-1)%9) + 9;
        }
        return (count-1)%9 +1;
    }

    public static void main(String[] args) {
        int num=971131546,x=966980466,y=531910024;
        System.out.println(new UnionFind().orchestraLayout(num,x,y));

        //971131546
        //966980466
        //531910024
    }
}
